;-------------------------------------------------------------------------------
format ELF64 executable
entry start

SDL_OPENGL = 0x04000000
SDL_NOFRAME = 0x02000000
SDL_KEYDOWN = 0x300

GL_FRAGMENT_SHADER = 0x8B30

DEBUG = 0

macro emit [param] {param}
;-------------------------------------------------------------------------------
include 'import64.inc'
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libdl.so.2'
import dlopen,dlsym
;-------------------------------------------------------------------------------
segment readable writeable executable

start:
    sub rsp,2048
    ; define local variables
    emit <event equ rsp+1024>,<log equ rsp>
    mov r13,[dlsym]
if DEBUG = 1
    ; load libc
    emit <mov rdi,g_str_libc>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov [g_libc],rax>
    emit <mov rdi,[g_libc]>,<mov rsi,g_str_printf>,<call r13>,<mov [printf],rax>
end if
    ; load SDL library
    emit <mov rdi,g_str_libsdl>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov r12,rax>
    ; SDL_SetVideoMode
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_SetVideoMode>,<call r13>
    emit <mov edi,1280>,<mov esi,720>,<xor edx,edx>,<mov ecx,SDL_OPENGL+SDL_NOFRAME>,<call rax>
    ; load needed SDL functions
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GL_GetProcAddress>,<call r13>,<mov rbx,rax>
    ; create program
    emit <mov rdi,g_str_glCreateShaderProgramv>,<call rbx>
    emit <mov edi,GL_FRAGMENT_SHADER>,<mov esi,1>,<mov rdx,g_src_fs_ptr>,<call rax>,<mov r15d,eax>
    ; use program
    emit <mov rdi,g_str_glUseProgram>,<call rbx>
    emit <mov edi,r15d>,<call rax>
if DEBUG = 1
    ; print GLSL info log
    emit <mov rdi,g_str_glGetProgramInfoLog>,<call rbx>
    emit <mov edi,r15d>,<mov esi,1024>,<xor edx,edx>,<lea rcx,[log]>,<call rax>
    emit <mov rdi,g_str_log>,<lea rsi,[log]>,<call [printf]>
end if
      .main_loop:
    ; process window events
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_PollEvent>,<call r13>
    emit <lea rdi,[event]>,<call rax>
    cmp dword [event],SDL_KEYDOWN
    je .main_loop_end
    ; get time
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GetTicks>,<call r13>,<call rax>,<mov r14d,eax>
    emit <mov rdi,g_str_glTexCoord1f>,<call rbx>
    emit <cvtsi2ss xmm0,r14d>,<call rax>
    ; draw
    emit <mov rdi,g_str_glRecti>,<call rbx>
    emit <mov edi,-1>,<mov esi,-1>,<mov edx,1>,<mov ecx,1>,<call rax>
    ; swap
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GL_SwapBuffers>,<call r13>,<call rax>
    jmp .main_loop
      .main_loop_end:
    ; terminate process
    emit <mov eax,60>,syscall
;-------------------------------------------------------------------------------
align 1
g_src_fs:
db  'float t;'
db  'void main(){'
db      'vec2 p=-1.+2.*gl_FragCoord.xy/vec2(1280,720);'
db      'float a=gl_TexCoord[0].s*.04;'
db      'float d,e,f,g=1./40.,h,i,r,q;'
db      'e=400.*(p.x*.5+.5);'
db      'f=400.*(p.y*.5+.5);'
db      'i=200.+sin(e*g+a/150.)*20.;'
db      'd=200.+cos(f*g/2.)*18.+cos(e*g)*7.;'
db      'r=sqrt((i-e)*(i-e)+(d-f)*(d-f));'
db      'q=f/r;'
db      'e=(r*cos(q))-a/2.;f=(r*sin(q))-a/2.;'
db      'd=sin(e*g)*176.+sin(e*g)*164.+r;'
db      'h=((f+d)+a/2.)*g;'
db      'i=cos(h+r*p.x/1.3)*(e+e+a)+cos(q*g*6.)*(r+h/3.);'
db      'h=sin(f*g)*144.-sin(e*g)*212.*p.x;'
db      'h=(h+(f-e)*q+sin(r-(a+h)/7.)*10.+i/4.)*g;'
db      'i+=cos(h*2.3*sin(a/350.-q))*184.*sin(q-(r*4.3+a/12.)*g)+tan(r*g+h)*184.*cos(r*g+h);'
db      'i=mod(i/5.6,256.)/64.;'
db      'if(i<0.)i+=4.;'
db      'if(i>=2.)i=4.-i;'
db      'd=r/350.;'
db      'd+=sin(d*d*8.)*.52;'
db      'f=(sin(a*g)+1.)/2.;'
db      'gl_FragColor=vec4(vec3(f*i/1.6,i/2.+d/13.,i)*d*p.x+vec3(i/1.3+d/8.,i/2.+d/18.,i)*d*(1.-p.x),1.);'
db  '}',0

g_str_libsdl db 'libSDL-1.3.so.0',0
g_str_SDL_SetVideoMode db 'SDL_SetVideoMode',0
g_str_SDL_PollEvent db 'SDL_PollEvent',0
g_str_SDL_GL_SwapBuffers db 'SDL_GL_SwapBuffers',0
g_str_SDL_GetTicks db 'SDL_GetTicks',0
g_str_SDL_GL_GetProcAddress db 'SDL_GL_GetProcAddress',0
g_str_glCreateShaderProgramv db 'glCreateShaderProgramv',0
g_str_glRecti db 'glRecti',0
g_str_glUseProgram db 'glUseProgram',0
g_str_glTexCoord1f db 'glTexCoord1f',0
if DEBUG = 1
g_str_libc db 'libc.so.6',0
g_str_printf db 'printf',0
g_str_glGetProgramInfoLog db 'glGetProgramInfoLog',0
g_str_log db 'GLSL info log:',10,'%s',10,0
g_str_d db '%d',10,0
g_str_p db '%p',10,0
end if

align 8
g_src_fs_ptr dq g_src_fs

if DEBUG = 1
g_libc dq 0
printf dq 0
end if
;-------------------------------------------------------------------------------
